﻿using System;
using System.Collections.Generic;
using System.Text;
using NVelocity.Runtime;
using NVelocity.App;
using NVelocity.Context;
using Commons.Collections;
using System.IO;

namespace NVelocity
{
    /// <summary>
    /// NVelocity模板工具类 NVelocityHelper
    /// </summary>
    public class NVelocityHelper
    {
        private VelocityEngine velocity = null;
        private IContext context = null;
        private string templateDir = "Template"; //无参数初始化时默认路径

        #region 构造函数及初始化函数
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="ps_TemplateDir">模板文件夹路径</param>
        public NVelocityHelper(string ps_TemplateDir)
        {
            templateDir = ps_TemplateDir;
            Init();
        }
        /// <summary>
        /// 无参数构造函数
        /// </summary>
        public NVelocityHelper() { Init(); }

        /// <summary>
        /// 初始化NVelocity模块 
        /// create by liubiqu@hotmail.com 2010-04-14
        /// </summary>
        private void Init()
        {
            //创建VelocityEngine实例对象
            velocity = new VelocityEngine();

            //使用设置初始化VelocityEngine
            ExtendedProperties props = new ExtendedProperties();

            props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");
            props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, AppDomain.CurrentDomain.BaseDirectory + templateDir);
            props.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8");
            props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8");

            props.AddProperty(RuntimeConstants.COUNTER_NAME, "index");

            props.AddProperty(RuntimeConstants.VM_LIBRARY, null);

            props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, true);              //是否缓存

            props.AddProperty("file.resource.loader.modificationCheckInterval", (Int64)10);    //缓存时间(秒)
            
            velocity.Init(props);

            //为模板变量赋值
            context = new VelocityContext();
            
           
        }
        #endregion
        public void Edit(string key, object value)
        {
            if (context == null)
                return;
            if (context.ContainsKey(key))
            {
                context.Remove(key);
                context.Put(key, value);
            }
        
        }
        public void Put(string key, object value)
        {
            if (context == null)
                context = new VelocityContext();
            context.Put(key, value);
        }
        public void Add(string key, object value)
        {
            if (context == null)
                context = new VelocityContext();
            context.Put(key, value);
        }
        /// <summary>
        /// 通过获得模板文件生成结果字符串
        /// </summary>
        /// <param name="ps_TemplateFileName">模板文件名，为从模板目录开始的完整路径。即相对路径。如cms/news_view.htm</param>
        public StringBuilder GetStringFromVm(string ps_TemplateFileName)
        {
            Template template = velocity.GetTemplate(ps_TemplateFileName);

            StringWriter writer = new StringWriter();
            template.Merge(context, writer);
            writer.Flush();
            writer.Close();
            writer.Dispose();
            return writer.GetStringBuilder();

        }

        /// <summary>
        /// 生成字符串内容
        /// </summary>
        /// <param name="stringExpression">要解析的字符串数据</param>
        /// <param name="logTag">Log标识</param>
        /// <returns></returns>
        public StringBuilder GetStringFromSource(string ps_SourceStr)
        {
            StringWriter writer = new StringWriter();
            velocity.Evaluate(context, writer, "log", ps_SourceStr);
            writer.Flush();
            writer.Close();
            writer.Dispose();
            return writer.GetStringBuilder();
        }

        #region 静态封装

        private static NVelocityHelper ve;
        /// <summary>
        /// 静态模板类
        /// </summary>
        /// <param name="ps_TemplateDir">模板文件夹路径</param>
        /// <returns></returns>
        public static NVelocityHelper getNVE(string ps_TemplateDir)
        {
            if (ve == null)
            {
                ve = new NVelocityHelper(ps_TemplateDir);
            }
            return ve;

        }

        public static StringBuilder GetStringFromVmStatic(string ps_TemplateFileName)
        {
            return ve.GetStringFromVm(ps_TemplateFileName);
        }

        public StringBuilder GetStringFromSourceStatic(string ps_SourceStr)
        {

            return ve.GetStringFromSource(ps_SourceStr);
        }

        #endregion
    }
}
